#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->openGLWidget);
    connect(ui->openGLWidget,SIGNAL(mousePickingPos(QVector3D)),
            this,SLOT(getMousePickingPos(QVector3D)));

}

MainWindow::~MainWindow()
{
    delete ui;
}
#include <QFileDialog>
void MainWindow::on_actDebug_triggered()
{
    //get json测试
//    QNetworkRequest request;
//    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
//    QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
//    Q_ASSERT(connRet);
//    request.setUrl(QUrl("http://119.3.243.178:7201/approval/deviceInfo/validate?mac=2C16DBA44651"));
//    QNetworkReply* reply = naManager->get(request);

//    qDebug()<<'i';

    return;

    QString mURL="http://119.3.243.178:7201/approval/zwd/add";
    QString strBody;
    strBody.append("{");
    strBody.append("\"deviceNum\":\"test\",");
    strBody.append("\"businessName\":\"测试业务\"");
    strBody.append("}");
    post(mURL,"mURL",strBody);
}

int MainWindow::post(QString& mURL, QString param, QString body)
{
    QNetworkRequest request;
    QByteArray result;
    QEventLoop eventLoop;
    request.setUrl(QUrl(mURL));
    if (mURL.startsWith("https://"))
    {
        // 方案1
        QSslConfiguration conf = request.sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        conf.setProtocol(QSsl::TlsV1SslV3);
        request.setSslConfiguration(conf);
        // 方案2
        //request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
        //reply->ignoreSslErrors();
    }

    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json;charset=UTF-8");

    QByteArray append(body.toLocal8Bit());
    QNetworkAccessManager* mpManager = new QNetworkAccessManager(this);
    QNetworkReply* reply = mpManager->post(request, append);
    QObject::connect(mpManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    eventLoop.exec();

    QVariant code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if (!code.isNull() && code.toInt() == 200)
    {
        result = reply->readAll();
        qDebug() << "Code: " << code.toInt() << "response: " << result;
        //response = result;
        return code.toInt();
    }
    else if (!code.isNull())
    {
        qDebug() << "Code: " << code.toInt();
        return code.toInt();
    }
    qDebug() << "reply error: " << reply->errorString();
    return -1;
}


void MainWindow::on_actLoadModel_triggered()
{
    //QString str=QFileDialog::getOpenFileName(this,"选择模型文件","","OBJ (*.obj);;FBX(*.fbx);;ALL FILES( *.* ) ");
    // MainWindow::loadModels(str);
}
void MainWindow::loadModels(QString str){
   // str.remove(0,1);
    qDebug()<<str<<endl;
    ui->openGLWidget->loadModel(str.toStdString());
}

void MainWindow::getMousePickingPos(QVector3D pos)
{
    ui->statusBar->setStyleSheet("font: 14pt ");
    ui->statusBar->showMessage(" 世界坐标:    X:"
                               +QString::number(pos.x(),'f', 2)
                               +" Y:"+QString::number(pos.y(),'f', 2)
                               +" Z:"+QString::number(pos.z(),'f', 2)
                               );
}
